package cn.project.dao;

import cn.project.model.User;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Component("userDao")
public class UserDaoImpl implements UserDao {
    @Resource(name = "dataSource")
    private DataSource dataSource;

    public List<User> getAllUser() {
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<User>list=new ArrayList <User>();
        try {
           connection= dataSource.getConnection();
           String sql="select * from user";
           ps=connection.prepareStatement(sql);
           rs=ps.executeQuery();
                while (rs.next()){
                    User user=new User();
                    user.setUserid(rs.getInt("userid"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                list.add(user);
                }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}
